package com.along.dev.rbac.service;


import com.along.dev.rbac.dto.DeptDTO;
import com.along.dev.rbac.entity.Dept;
import com.along.dev.rbac.mapper.DeptMapper;
import com.along.dev.rbac.qo.DeptQO;
import com.along.utility.common.utils.BeanUtils;
import com.along.utility.common.utils.JsonUtils;
import com.along.utility.common.utils.ListCopier;
import com.along.utility.page.mybatis.Paging;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Date;
import java.util.List;

/**
 * @Author liujinyi
 * @Date 2024/12/19 17:33
**/
@Service
public class DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;

    private static final Logger logger = LoggerFactory.getLogger(DeptService.class);


    public List<Dept> exclude(Integer deptId) {
        return deptMapper.exclude(deptId);
    }

    @Paging
    public Page<DeptDTO> find(DeptQO deptQO, Integer page, Integer rows) {
        return ListCopier.pageCopy(this.deptMapper.find(deptQO), DeptDTO.class);
    }

    @Transactional(rollbackFor = Exception.class)
    public void save(DeptDTO deptDTO) {
        Date date = new Date();
        deptDTO.setCreateTime(date);
        deptDTO.setUpdateTime(date);
        Dept dept = BeanUtils.copy(deptDTO, new Dept());
        int size = deptMapper.save(dept);
        logger.info("保存部门信息: {}, result: {}", JsonUtils.write(deptDTO), size);
    }

    /**
     * 使用编程式注解加事务
     * @Author liujinyi
     * @Date 2025/7/4 11:41
     * @Param  * @param deptDTO
     * @Return void
    **/
    public void saveTwo(DeptDTO deptDTO) {
        transactionTemplate.execute(status -> {
            try {
                Date date = new Date();
                deptDTO.setCreateTime(date);
                deptDTO.setUpdateTime(date);
                Dept dept = BeanUtils.copy(deptDTO, new Dept());
                int size = deptMapper.save(dept);
                logger.info("保存部门信息: {}, result: {}", JsonUtils.write(deptDTO), size);
                return size;
            } catch (Exception e) {
                status.setRollbackOnly();
                logger.error("保存部门信息失败: {}", JsonUtils.write(deptDTO), e);
                throw e; // 或者返回特定值，根据业务需求决定
            }
        });
    }




    @Transactional(rollbackFor = Exception.class)
    public void update(DeptDTO deptDTO) {
        deptDTO.setUpdateTime(new Date());
        Dept dept = BeanUtils.copy(deptDTO, new Dept());
        int size = deptMapper.update(dept);
        logger.info("修改部门信息: {},result: {}", JsonUtils.write(deptDTO), size);
    }

    public void delete(DeptDTO deptDTO) {
        int result = deptMapper.delete(deptDTO.getDeptId());
        logger.info("删除部门信息：deptId: {}, result: {}", JsonUtils.write(deptDTO.getDeptId()), result);
    }
    public List<Dept> commFindByName(Long deptId) {
        return deptMapper.commFindByName(deptId);
    }
}
